﻿
namespace ATA.Windows.Forms
{
	/// <summary>
	/// abstract HoiVien typed datagridview
	/// </summary>
	[System.ComponentModel.DesignerCategoryAttribute("code")]
	public abstract class HoiVienDataGridViewBase : System.Windows.Forms.UserControl
	{	
		/// <summary> 
		/// Thrown when the current Entity changed.
		/// </summary>
		public event System.EventHandler<HoiVienDataGridViewEventArgs> CurrentEntityChanged;
		
		#region Fields
				
		/// <summary> 
		/// the current entity.
		/// </summary>
		private Entities.HoiVien _currentHoiVien = null;
		//private Dictionary<string, string> _columnsHeaders = new Dictionary<string, string>();
		
		/// <summary> 
		/// the datagridview instance
		/// </summary>
		protected System.Windows.Forms.DataGridView uxHoiVienDataGridView;
		
		/// <summary> 
		/// the errorprovider
		/// </summary>
		protected System.Windows.Forms.ErrorProvider uxHoiVienErrorProvider;
		
		/// <summary> 
		/// the bindingsource
		/// </summary>
		protected System.Windows.Forms.BindingSource uxHoiVienBindingSource;
		
		
		/// <summary> 
		/// the DGV column associated with the HoiVienId property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxHoiVienIdDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the KhachHangId property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxKhachHangIdDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the LoaiHoiVien property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxLoaiHoiVienDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the MaSoHv property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxMaSoHvDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the NgayDangKy property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxNgayDangKyDataGridViewColumn;
		
		#endregion
					
		#region Combo list
		
		#endregion
		
		#region Main Datasource
		
		private Entities.TList<Entities.HoiVien> _HoiVienList;
				
		/// <summary> 
		/// The list of HoiVien to be displayed in the DGV.
		/// </summary>
		[System.ComponentModel.Category("Data")]
		public Entities.TList<Entities.HoiVien> HoiVienList
		{
			get {return this._HoiVienList;}
			set
			{
				this._HoiVienList = value;
				this.uxHoiVienBindingSource.DataSource = null;
				this.uxHoiVienBindingSource.DataSource = value;
				this.uxHoiVienDataGridView.DataSource = null;
				this.uxHoiVienDataGridView.DataSource = this.uxHoiVienBindingSource;				
				//this.uxHoiVienBindingSource.ListChanged += new System.ComponentModel.ListChangedEventHandler(uxHoiVienBindingSource_ListChanged);
				this.uxHoiVienBindingSource.CurrentItemChanged += new System.EventHandler(OnHoiVienBindingSourceCurrentItemChanged);
			}
		}
		
		private void OnHoiVienBindingSourceCurrentItemChanged(object sender, System.EventArgs e)
		{
			_currentHoiVien = uxHoiVienBindingSource.Current as Entities.HoiVien;
			
			if (_currentHoiVien != null)
			{
				_currentHoiVien.Validate();
			}
			//_HoiVien.Validate();
			OnCurrentEntityChanged();
		}

		//void uxHoiVienBindingSource_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
		//{
			//throw new Exception("The method or operation is not implemented.");
		//}		
		
		#endregion
		
		/// <summary>
		/// Gets the currently selected <c cref="Entities.HoiVien"/> instance.
		/// </summary>
		public Entities.HoiVien SelectedHoiVien
		{
			get {return this._currentHoiVien;}
		}
		
		#region other properties
		
		/// <summary>
		/// Gets or sets a value indicating whether you have provided your own data-management operations for the DataGridView control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool VirtualMode
		{
			get {return this.uxHoiVienDataGridView.VirtualMode;}
			set
			{
				this.uxHoiVienDataGridView.VirtualMode = value;
			}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the option to add rows is displayed to the user.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToAddRows
		{
			get {return this.uxHoiVienDataGridView.AllowUserToAddRows;}
			set {this.uxHoiVienDataGridView.AllowUserToAddRows = value;}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the user is allowed to delete rows from the DataGridView.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToDeleteRows
		{
			get {return this.uxHoiVienDataGridView.AllowUserToDeleteRows;}
			set {this.uxHoiVienDataGridView.AllowUserToDeleteRows = value;}
		}
		
		/// <summary>
		/// Gets the <c cref="DataGridView"/> instance.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public System.Windows.Forms.DataGridView DataGridView
		{
			get {return this.uxHoiVienDataGridView;}
		}
		
		/// <summary>
		/// Gets a collection that contains all the columns in the control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]		
		[System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Visible)]
		//[System.ComponentModel.EditorAttribute("System.Windows.Forms.Design.DataGridViewColumnCollectionEditor, System.Design", typeof(System.Drawing.Design.UITypeEditor))] 
		public System.Windows.Forms.DataGridViewColumnCollection Columns
		{
			get { return this.uxHoiVienDataGridView.Columns; }
		}
		
		#endregion
		
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;
	
		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}
	
		#region Constructor
	
		/// <summary>
		/// Initializes a new instance of the <see cref="HoiVienDataGridViewBase"/> class.
		/// </summary>
		public HoiVienDataGridViewBase()
		{			
			this.components = new System.ComponentModel.Container();			
			
			this.uxHoiVienDataGridView = new System.Windows.Forms.DataGridView();
			this.uxHoiVienBindingSource = new System.Windows.Forms.BindingSource(this.components);
			this.uxHoiVienErrorProvider = new System.Windows.Forms.ErrorProvider(this.components);
			this.uxHoiVienIdDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxKhachHangIdDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxLoaiHoiVienDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxMaSoHvDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxNgayDangKyDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			((System.ComponentModel.ISupportInitialize)(this.uxHoiVienDataGridView)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxHoiVienBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxHoiVienErrorProvider)).BeginInit();
			this.SuspendLayout();
						
			// 
			// uxHoiVienErrorProvider
			// 
			this.uxHoiVienErrorProvider.ContainerControl = this;
			this.uxHoiVienErrorProvider.DataSource = this.uxHoiVienBindingSource;						
			// 
			// uxHoiVienDataGridView
			// 
			this.uxHoiVienDataGridView.AutoGenerateColumns = false;
			this.uxHoiVienDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.uxHoiVienDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
		this.uxHoiVienIdDataGridViewColumn,
		this.uxKhachHangIdDataGridViewColumn,
		this.uxLoaiHoiVienDataGridViewColumn,
		this.uxMaSoHvDataGridViewColumn,
		this.uxNgayDangKyDataGridViewColumn			});
			this.uxHoiVienDataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
			this.uxHoiVienDataGridView.Location = new System.Drawing.Point(0, 0);
			this.uxHoiVienDataGridView.Name = "uxHoiVienDataGridView";
			this.uxHoiVienDataGridView.Size = new System.Drawing.Size(470, 300);
			this.uxHoiVienDataGridView.TabIndex = 0;	
			this.uxHoiVienDataGridView.BackgroundColor = System.Drawing.Color.WhiteSmoke;
			this.uxHoiVienDataGridView.EnableHeadersVisualStyles = false;
			this.uxHoiVienDataGridView.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.OnHoiVienDataGridViewDataError);
			this.uxHoiVienDataGridView.CellValueNeeded += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnHoiVienDataGridViewCellValueNeeded);
			this.uxHoiVienDataGridView.CellValuePushed += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnHoiVienDataGridViewCellValuePushed);
			
			//
			// uxHoiVienIdDataGridViewColumn
			//
			this.uxHoiVienIdDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxHoiVienIdDataGridViewColumn.DataPropertyName = "HoiVienId";
			this.uxHoiVienIdDataGridViewColumn.HeaderText = "HoiVienId";
			this.uxHoiVienIdDataGridViewColumn.Name = "uxHoiVienIdDataGridViewColumn";
			this.uxHoiVienIdDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxHoiVienIdDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxHoiVienIdDataGridViewColumn.ReadOnly = false;		
			//
			// uxKhachHangIdDataGridViewColumn
			//
			this.uxKhachHangIdDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxKhachHangIdDataGridViewColumn.DataPropertyName = "KhachHangId";
			this.uxKhachHangIdDataGridViewColumn.HeaderText = "KhachHangId";
			this.uxKhachHangIdDataGridViewColumn.Name = "uxKhachHangIdDataGridViewColumn";
			this.uxKhachHangIdDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxKhachHangIdDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxKhachHangIdDataGridViewColumn.ReadOnly = false;		
			//
			// uxLoaiHoiVienDataGridViewColumn
			//
			this.uxLoaiHoiVienDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxLoaiHoiVienDataGridViewColumn.DataPropertyName = "LoaiHoiVien";
			this.uxLoaiHoiVienDataGridViewColumn.HeaderText = "LoaiHoiVien";
			this.uxLoaiHoiVienDataGridViewColumn.Name = "uxLoaiHoiVienDataGridViewColumn";
			this.uxLoaiHoiVienDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxLoaiHoiVienDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxLoaiHoiVienDataGridViewColumn.ReadOnly = false;		
			//
			// uxMaSoHvDataGridViewColumn
			//
			this.uxMaSoHvDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxMaSoHvDataGridViewColumn.DataPropertyName = "MaSoHv";
			this.uxMaSoHvDataGridViewColumn.HeaderText = "MaSoHv";
			this.uxMaSoHvDataGridViewColumn.Name = "uxMaSoHvDataGridViewColumn";
			this.uxMaSoHvDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxMaSoHvDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxMaSoHvDataGridViewColumn.ReadOnly = false;		
			//
			// uxNgayDangKyDataGridViewColumn
			//
			this.uxNgayDangKyDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxNgayDangKyDataGridViewColumn.DataPropertyName = "NgayDangKy";
			this.uxNgayDangKyDataGridViewColumn.HeaderText = "NgayDangKy";
			this.uxNgayDangKyDataGridViewColumn.Name = "uxNgayDangKyDataGridViewColumn";
			this.uxNgayDangKyDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxNgayDangKyDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxNgayDangKyDataGridViewColumn.ReadOnly = false;		
			
			// this
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.Controls.Add(this.uxHoiVienDataGridView);
			this.Name = "HoiVienDataGridView";
			this.Size = new System.Drawing.Size(470, 300);
			((System.ComponentModel.ISupportInitialize)(this.uxHoiVienErrorProvider)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxHoiVienDataGridView)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxHoiVienBindingSource)).EndInit();
			this.ResumeLayout(false);
		}
		#endregion
				
		#region events
		
		/// <summary>
		/// Raised the CurrentEntityChanged event.
		/// </summary>
		protected void OnCurrentEntityChanged()
		{
			if (CurrentEntityChanged != null)
			{
				HoiVienDataGridViewEventArgs args = new HoiVienDataGridViewEventArgs();
				args.HoiVien = _currentHoiVien;
				CurrentEntityChanged(this, args); 
			}
		}
		
		#endregion
		
		/// <summary>
		/// Strongly typed event 
		/// </summary>
		public class HoiVienDataGridViewEventArgs : System.EventArgs
		{
			private Entities.HoiVien	_HoiVien;
	
			/// <summary>
			/// the  Entities.HoiVien instance.
			/// </summary>
			public Entities.HoiVien HoiVien
			{
				get { return _HoiVien; }
				set { _HoiVien = value; }
			}	
		}
		
		/// <summary>
		/// Handles the DataError event of the uxHoiVienDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewDataErrorEventArgs"/> instance containing the event data.</param>
		protected void OnHoiVienDataGridViewDataError(object sender, System.Windows.Forms.DataGridViewDataErrorEventArgs e)
		{
			System.Diagnostics.Debug.WriteLine(string.Format( "{0}, {1}", e.ColumnIndex, e.RowIndex));
		}
		
		/// <summary>
		/// Handles the CellValueNeeded event of the uxHoiVienDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnHoiVienDataGridViewCellValueNeeded(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxHoiVienDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxHoiVienIdDataGridViewColumn":
						e.Value = HoiVienList[e.RowIndex].HoiVienId;
						break;
					case "uxKhachHangIdDataGridViewColumn":
						e.Value = HoiVienList[e.RowIndex].KhachHangId;
						break;
					case "uxLoaiHoiVienDataGridViewColumn":
						e.Value = HoiVienList[e.RowIndex].LoaiHoiVien;
						break;
					case "uxMaSoHvDataGridViewColumn":
						e.Value = HoiVienList[e.RowIndex].MaSoHv;
						break;
					case "uxNgayDangKyDataGridViewColumn":
						e.Value = HoiVienList[e.RowIndex].NgayDangKy;
						break;
				default:
				break;
			}
		}
		
		/// <summary>
		/// Handles the CellValuePushed event of the uxHoiVienDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnHoiVienDataGridViewCellValuePushed(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxHoiVienDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxHoiVienIdDataGridViewColumn":
						HoiVienList[e.RowIndex].HoiVienId = (System.Guid)e.Value;
						break;
					case "uxKhachHangIdDataGridViewColumn":
						HoiVienList[e.RowIndex].KhachHangId = (System.Guid)e.Value;
						break;
					case "uxLoaiHoiVienDataGridViewColumn":
						HoiVienList[e.RowIndex].LoaiHoiVien = (System.Int16?)e.Value;
						break;
					case "uxMaSoHvDataGridViewColumn":
						HoiVienList[e.RowIndex].MaSoHv = (System.String)e.Value;
						break;
					case "uxNgayDangKyDataGridViewColumn":
						HoiVienList[e.RowIndex].NgayDangKy = (System.DateTime)e.Value;
						break;
				default:
				break;
			}
		}
	}
}
